﻿using System;
using System.Collections.Generic;
using System.Linq;

using HelfenHelfen.DataAccess.Entities;
using HelfenHelfen.DataAccess.Interfaces;

namespace HelfenHelfen.DataAccess
{
    public class OrganisationDataAccess : IOrganisationDataAccess
    {
        public void Insert(Entities.Organisation newOrganisation)
        {
            using (HelfenHelfenEntities db = new HelfenHelfenEntities())
            {
                db.Organisations.AddObject(newOrganisation);
                db.SaveChanges();
            }
        }

        public void Update(Entities.Organisation updateOrganisation)
        {
            using (HelfenHelfenEntities db = new HelfenHelfenEntities())
            {
                db.ApplyChanges("Organisation", updateOrganisation);
                db.SaveChanges();
            }
        }

        public void Delete(Guid organisationId)
        {
            using (HelfenHelfenEntities db = new HelfenHelfenEntities())
            {
                var obj = (from o in db.Organisations
                             where o.OrganisationGUID == organisationId
                             select o).FirstOrDefault();

                if (obj != null)
                {
                    db.DeleteObject(obj);
                    db.SaveChanges();
                }

            }
        }

        public Organisation Get(Guid organisationId)
        {
            using (HelfenHelfenEntities db = new HelfenHelfenEntities())
            {
                var obj = (from o in db.Organisations
                           where o.OrganisationGUID == organisationId
                           select o).FirstOrDefault();
                return obj;
            }
        }

        public IList<Organisation> GetAll()
        {
            using (HelfenHelfenEntities db = new HelfenHelfenEntities())
            {
                return db.Organisations.ToList();
            }
        }
    }
}
